﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace Zhongy
{
    public class Menu:MonoBehaviour
    {

        [MenuItem("Tools/Zy/Settings")]
        public static void SettingMacor()
        {
            SettingMacro settingMacro = new SettingMacro();
            settingMacro.titleContent = new GUIContent("MACOR SETTINGS");
            settingMacro.Show();
        }
    }

    public class SettingMacro : EditorWindow
    {
        public const string DEBUG_MACOR = "DEBUG_MODEL";
        public const string PRINT3AND4_MACOR = "PRINT3AND4";
        public const string PRINT12_MACOR = "PRINT12";
        public const string NOVALIDITY_MACOR = "NOVALIDITY";
        public const string DEBUG_BROAD = "DEBUG_BROAD";

        private List<MacorItem> macorLst = new List<MacorItem>();
        private Dictionary<string, bool> dicName2On = new Dictionary<string, bool>();
        private string strMacor = string.Empty;

        void OnEnable()
        {
            strMacor = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
            macorLst.Clear();

            macorLst.Add(new MacorItem()
            {
                Name = DEBUG_MACOR, DisplayName = "调试模式", IsDebug = true, IsRelease = false
            });
            macorLst.Add(new MacorItem()
            {
                Name = DEBUG_BROAD,
                DisplayName = "打印广播",
                IsDebug = true,
                IsRelease = false
            });

            macorLst.Add(new MacorItem()
            {
                Name = NOVALIDITY_MACOR,
                DisplayName = "忽略数据有效性",
                IsDebug = true,
                IsRelease = false
            });

            macorLst.Add(new MacorItem()
            {
                Name = PRINT3AND4_MACOR, DisplayName = "打印MT3和MT4", IsDebug = true, IsRelease = false
            });

            macorLst.Add(new MacorItem()
            {
                Name = PRINT12_MACOR,
                DisplayName = "打印MT12",
                IsDebug = true,
                IsRelease = false
            });

            foreach (var item in macorLst)
            {
                dicName2On[item.Name] = !string.IsNullOrEmpty(strMacor) && strMacor.Contains(item.Name);
            }
        }

        void OnGUI()
        {
            for (int i = 0; i < macorLst.Count; i++)
            {
                EditorGUILayout.BeginHorizontal("box");
                dicName2On[macorLst[i].Name] =
                    GUILayout.Toggle(dicName2On[macorLst[i].Name], new GUIContent(macorLst[i].DisplayName));
                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("保存", GUILayout.Width(100)))
            {
                SaveMacor();
            }

            if (GUILayout.Button("调试模式", GUILayout.Width(100)))
            {
                foreach (var item in macorLst)
                {
                    dicName2On[item.Name] = item.IsDebug;
                }

                SaveMacor();
            }

            if (GUILayout.Button("调试模式no3&4", GUILayout.Width(100)))
            {
                foreach (var item in macorLst)
                {
                    if (item.Name.Equals("PRINT3AND4", StringComparison.CurrentCulture))
                        dicName2On[item.Name] = false;
                    else
                    {
                        dicName2On[item.Name] = item.IsDebug;
                    }
                }

                SaveMacor();
            }

            if (GUILayout.Button("调试模式no3&4&12", GUILayout.Width(120)))
            {
                foreach (var item in macorLst)
                {
                    if (item.Name.Equals("PRINT3AND4", StringComparison.CurrentCulture))
                        dicName2On[item.Name] = false;
                    else if (item.Name.Equals("PRINT12", StringComparison.CurrentCulture))
                        dicName2On[item.Name] = false;
                    else
                    {
                        dicName2On[item.Name] = item.IsDebug;
                    }
                }

                SaveMacor();
            }

            if (GUILayout.Button("发布模式", GUILayout.Width(100)))
            {
                foreach (var item in macorLst)
                {
                    dicName2On[item.Name] = item.IsRelease;
                }

                SaveMacor();
            }

            EditorGUILayout.EndHorizontal();
        }

        void SaveMacor()
        {
            strMacor = string.Empty;
            foreach (var item in dicName2On)
            {
                if (item.Value)
                    strMacor += string.Format("{0};", item.Key);
            }

            PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android,strMacor);
            PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone,strMacor);
            PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS,strMacor);
        }
    }

    public class MacorItem
    {
        public string Name;
        public string DisplayName;
        public bool IsDebug;
        public bool IsRelease;
    }
}